/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package user

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/redishelper"

	//"github.com/mjiulee/lego/utils"
)


const REDIS_WX_USER_BY_ID_PREFIX string = "redis_wx_user_by_id:"
const REDIS_WX_USER_BY_OPEN_ID_PREFIX string = "redis_wx_user_by_open_id:"
const REDIS_WX_USER_BY_UNION_ID_PREFIX string = "redis_wx_user_by_uion_id:"

/* 根据id获取记录
 * -----------------
 */
func (t *WxUser) RecodeById(id int64) *WxUser {
	item := new(WxUser)

	err := redishelper.RedisGetKeyEx(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(id), item)
	if err == nil {
		return item
	}


	_, err = lego.GetDBEngine().ID(id).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	_ = redishelper.RedisSetKeyEx(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(id), item)
	return item
}

func (t *WxUser) RecodeByUnionId(unionId string) *WxUser {
	item := new(WxUser)


	err := redishelper.RedisGetKeyEx(REDIS_WX_USER_BY_UNION_ID_PREFIX+(unionId), item)
	if err == nil {
		return item
	}

	item.UnionId = unionId
	_, err = lego.GetDBEngine().Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}

	_ = redishelper.RedisSetKeyEx(REDIS_WX_USER_BY_OPEN_ID_PREFIX+(unionId), item)

	return item
}

func (t *WxUser) RecodeByOpenId(openid string/*, weappid string*/) *WxUser {
	item := new(WxUser)

	err := redishelper.RedisGetKeyEx(REDIS_WX_USER_BY_OPEN_ID_PREFIX+(openid)/*+("_")+(weappid)*/, item)
	if err == nil {
		return item
	}

	lego.GetDBEngine().Where("open_id=?", openid).Get(item)
	if item.Id <= 0 {
		return nil
	}

	_ = redishelper.RedisSetKeyEx(REDIS_WX_USER_BY_OPEN_ID_PREFIX+(openid)/*+("_")+(weappid)*/, item)
	return item
}

/* 添加
 * -----------------
 */
func (t *WxUser) AddRecode(item2add *WxUser) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(item2add.OpenId)/*+("_")+(item2add.WeappId)*/)
	return true
}

func (t *WxUser) AddRecodeWithsession(session *xorm.Session, item2add *WxUser) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(item2add.OpenId)/*+("_")+(item2add.WeappId)*/)

	return true
}

/* 删除(硬删除)*/
func (t *WxUser) DelRecodeById(id int64) bool {
	item := new(WxUser)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(id))
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(item.OpenId)/*+("_")+(item.WeappId)*/)
	return true
}

func (t *WxUser) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(WxUser)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(item.Id))
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(item.OpenId)/*+("_")+(item.WeappId)*/)
	return true
}

/* 更新
 * -----------------*/
func (t *WxUser) UpdateRecode(rc *WxUser) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).MustCols("gender").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(rc.Id))

	// TODO: 这里如果rc的WeppId的值为0的话，这对应的缓存不会被删除，这里要再考虑一下
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(rc.OpenId)/*+("_")+(rc.WeappId)*/)
	return true
}

func (t *WxUser) UpdateRecodeWithSession(session *xorm.Session, rc *WxUser) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_ID_PREFIX+utils.Int64ToString(rc.Id))
	_ = redishelper.RedisDelKey(REDIS_WX_USER_BY_OPEN_ID_PREFIX+string(rc.OpenId)/*+("_")+(rc.WeappId)*/)
	return true
}
